package com.tjmc.tiomsresource.schedule;

import com.tjmc.tiomsresource.collect.OmcResDBDataCleanJob;
import com.tjmc.tiomsresource.collect.OmcResFetchJob;
import com.tjmc.tiomsresource.sync.OmcResSyncJob;
import com.tjmc.tiomsresource.utils.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * @author Luc
 * @version 1.0
 * @date 2020-05-20 11:41
 * @description OMC北向接口资源采集调度
 */

@Service
public class ResCollectSchedule {

    private static Logger logger = LoggerFactory.getLogger(ResCollectSchedule.class);

    @Autowired
    private OmcResFetchJob omcResFetchJob;

    @Autowired
    private OmcResDBDataCleanJob omcResDBDataCleanJob;

    @Autowired
    private OmcResSyncJob omcResSyncJob;

    /**
     * @description:  每天1点、13点触发
     * @param:
     * @return:
     * @date: 2020/5/20
     */
    //    @Scheduled(cron = "0 */15 * * * ?") //每15分钟
//    @Scheduled(cron = "0 10 1,13 * * ?")
    @Scheduled(cron = "0 30 1 * * ?") //实际使用 1:30 开始采集
    public void collectOmcResource() {

        Date now = new Date();
        long time = System.currentTimeMillis();

        logger.info("1. OMC采集数据开始");
        String today = CommonUtils.utilDate2String(now, "yyyyMMdd", false);
        if(!omcResFetchJob.fetchCMData(today)) {
            logger.info("fetch OMC 数据错误");
            return;
        }
        logger.info("1. OMC采集数据结束");
        logger.info("2. OMC解压缩数据开始");
        List<File> unZipFiles;
        if((unZipFiles = omcResFetchJob.unZipCMData(today, now)) == null){
            logger.info("fetch OMC 数据错误");
            return;
        }
        logger.info("2. OMC解压缩数据结束");
        logger.info("3. OMC同步原始数据开始");
        omcResFetchJob.saveTranPtnHwDataToDB(unZipFiles);
        logger.info("3. OMC同步原始数据结束");

        logger.info("4. OMC清洗原始数据开始");
        omcResDBDataCleanJob.CleanData();
        logger.info("4. OMC清洗原始数据结束: " + (System.currentTimeMillis()-time)/1000 + "s");

        logger.info("5. Res本地数据库构建开始");
        time = System.currentTimeMillis();
        omcResSyncJob.buildTiDBData();
        logger.info("5. Res本地数据库构建结束: " + (System.currentTimeMillis()-time)/1000 + "s");

        omcResFetchJob.clearCMDataDir(now);
        logger.info("OMC 告警采集数据结束");

    }
}
